import { BaseResponse } from '@/common/BaseResponse';
import { HTTP_CUSTOM_CODE } from '@/enum/http-custom-code';
import { getUploadInstance } from '@/utils/upload';
import { UploadFile } from '@/utils/upload.interface';
import { HttpException, Injectable } from '@nestjs/common';

@Injectable()
export class UploadService {
  uploadFileInstance: UploadFile;

  constructor() {
    this.uploadFileInstance = getUploadInstance();
  }

  async uploadFile(file: Express.Multer.File, width: number, height: number) {
    try {
      let url = await this.uploadFileInstance.uploadFile(file);
      url += `&a=${JSON.stringify({ width, height })}`;
      console.log(url);
      return BaseResponse.toSucces(
        {
          url,
        },
        '上传成功',
      );
    } catch (error) {
      console.error(error);
      throw new HttpException('上传失败', HTTP_CUSTOM_CODE.UPLOAD_FILE_FAILURE);
    }
  }
}
